import 'package:get/get.dart';
import 'package:oramemo/core/controllers/device_controller.dart';
import 'package:oramemo/core/controllers/record_controller.dart';
import 'package:oramemo/pages/facility/facility_router.dart';
import 'package:oramemo/pages/transcription/files/controllers/transcriptio_files_logic.dart';

import 'ai_transcription_state.dart';

class AiTranscriptionLogic extends GetxController {
  final AiTranscriptionState state = AiTranscriptionState();
  final DeviceController deviceController = Get.find<DeviceController>();
  final RecordController controller = Get.find();
  final fileLogic = Get.put(TranscriptionFilesLogic());

  @override
  void onInit() async {
    await onLoad();
    super.onInit();
  }

  Future<void> onLoad() async {
    await requestDevices();

    requestData(page: 1);
  }

  Future<void> requestDevices() async {
    await deviceController.fetchDevices();
    if (deviceController.devices.isNotEmpty) {
      state.data = deviceController.devices[0];
    } else {
      // Get.toNamed(FacilityRouter.scanningPage);
    }
    update();
  }

  requestData({required int page}) async {
    String? voiceFolderId = fileLogic.currentFolder.value.id;
    bool res = await controller.fetchVoiceRecords(
        page: page, voiceFolderId: voiceFolderId);
    if (res) {
      state.page = page;
    }
    update();
  }
}
